<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="com.googlecode.contests_repo.core.model.Resource"%>
<%@page import="com.googlecode.contests_repo.core.WebTools"%>
<%@page import="java.util.Arrays"%>
<%@page import="com.googlecode.contests_repo.core.model.Contest"%>
<%@page import="com.googlecode.contests_repo.core.model.Problem"%>
<%@page import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="com.googlecode.contests_repo.core.users.AccessRight"%>
<%@page import="com.googlecode.contests_repo.core.users.UserManager"%>
<%@page import="com.googlecode.contests_repo.core.model.ResourcesUtils"%>
<%@page import="com.googlecode.contests_repo.core.model.DownloadUtils"%>
<%@page import="java.io.File"%>
<%@page import="java.util.List"%>


	<%
		if (request.getParameter("submittedProblem") != null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/ProblemEditServlet");
			dispatcher.include(request, response);
		}

		String problemId = request.getParameter("path");
		String contestId = problemId.substring(0,
				problemId.lastIndexOf(File.separator));
		String repoPath = ConfigurationManager.getInstance().getRepoRoot();
		String problemPath = repoPath + File.separator + problemId;
		String contestPath = repoPath + File.separator + contestId;
		String publicFolder = "_public_files";
		String privateFolder = "_files";
		Problem problem = ResourcesUtils.getJSON(new File(problemPath));
		if (problem == null) {
	%>
	<p>Requested problem does not exist.</p>
	<%
		} else {
			Contest contest = ResourcesUtils.getJSON(new File(contestPath));
			String loggedUser = (String) session.getAttribute("username");
			if (!ResourcesUtils.hasStarted(contest.getStartTime())
					&& !UserManager.hasAccessRight(loggedUser, problemPath,
							AccessRight.VIEW_FULL)) {
	%>
	<p>You do not have permission to view this problem.</p>
	<%
		} else if (UserManager.hasAccessRight(loggedUser, problemPath,
					AccessRight.VIEW)) {
				boolean canEdit = "1".equals(request.getParameter("edit"));
				boolean hasUserEditRights = UserManager.hasAccessRight(
						loggedUser, problemPath, AccessRight.VIEW_FULL);
				String readOnly = (!canEdit || !hasUserEditRights) ? "readonly=\"readonly\""
						: "";
	%>
	<h1>Problem Info</h1>

	<form method="post" action="">
		<p>
			<b>Id: </b> <input type="text" name="id"
				readonly="readonly" value="<%=problem.getId()%>" />
		</p>
		<p>
			<b>Title: </b> <input type="text"
				name="title" <%=readOnly%> value="<%=problem.getTitle()%>" />
			<input type="hidden" name="old-title" value="<%=problem.getTitle()%>" />
		</p>
		<p>
			<b>About: </b> <input type="text"
				name="about" <%=readOnly%> value="<%=problem.getAbout()%>" />
			<input type="hidden" name="old-about" value="<%=problem.getAbout()%>" />
		</p>
		<p>
			<b>Origin: </b> <input type="text"
				name="origin" <%=readOnly%> value="<%=problem.getOrigin()%>" />
			<input type="hidden" name="old-origin"
				value="<%=problem.getOrigin()%>" />
		</p>
		<p>
			<b>Authors: </b> <input type="text"
				name="authors" <%=readOnly%>
				value="<%=WebTools.arrayToString(problem.getAuthors())%>" /> <input
				type="hidden" name="old-authors"
				value="<%=WebTools.arrayToString(problem.getAuthors())%>" />
		</p>
		<p>
			<b>Test Weights: </b> <input type="text"
				name="test-weights" <%=readOnly%>
				value="<%=WebTools.arrayToString(problem.getTestWeights())%>" />
			<input type="hidden" name="old-test-weights"
				value="<%=WebTools.arrayToString(problem.getTestWeights())%>" />
		</p>
		<p>
			<b>Memory Limit: </b> <input type="text"
				name="memory-limit" <%=readOnly%>
				value="<%=problem.getMemoryLimit()%>" /> <input type="hidden"
				name="old-memory-limit" value="<%=problem.getMemoryLimit()%>" />
		</p>
		<p>
			<b>Time Limit: </b> <input type="text"
				name="time-limit" <%=readOnly%> value="<%=problem.getTimeLimit()%>" />
			<input type="hidden" name="old-time-limit"
				value="<%=problem.getTimeLimit()%>" />
		</p>
		<%
			if (hasUserEditRights) {
		%>
		<p>
			<b>Notes: </b> <input type="text"
				name="notes" <%=readOnly%> value="<%=problem.getNotes()%>" />
			<input type="hidden" name="old-notes" value="<%=problem.getNotes()%>" />
		</p>
		<%
			}
		%>
		<%
			if (canEdit && hasUserEditRights) {
		%>
		<p><input type="submit" name="submittedProblem" value="Change" />
		<a href="resource?path=<%=problemId%>">Discard changes</a>
		</p>
		<%
			}
		%>

	</form>

	<h2>Resources:</h2>
	<table>
		<tr>
			<th>Name</th>
			<th>Download</th>
			<%
				if (canEdit && hasUserEditRights) {
			%>
			<th>Delete</th>
			<%
				}
			%>
		</tr>
		<%
			List<File> resourcesList = DownloadUtils
							.fetchDownloadableResources(new File(problemPath));
					if (resourcesList != null && resourcesList.size() != 0) {
						for (File resource : resourcesList) {
							String resourcePath = problemPath + File.separator
									+ resource.getName();
							if (UserManager.hasAccessRight(loggedUser,
									problemPath, AccessRight.LIST)) {
		%>
		<tr>
			<td><%=resource.getName()%></td>
			<td><a
				href="download_problem_resources.jsp?path=<%=resourcePath%>">download</a>
			</td>

			<%
				if (canEdit && UserManager.hasAccessRight(
													loggedUser, problemPath,
													AccessRight.VIEW_FULL)) {
			%>
			<td><a
				href="delete_file?path=<%=problemId + File.separator
											+ resource.getName()%>">delete</a>
			</td>
			<%
				}
			%>
			</tr>
			<%
								}
							}
						}
			%>
	</table>
	<%
		}
		}
	%>
